import {
    batchDelete, deleteById, findAll, findPage, insert, updateById
} from '@/api/goods/brand';
// import pickerOptions from '@/utils/date';
// import base64 from "@/utils/base64Utils";
import showMessageBox from '@/utils/showMessageBox';

export default {
    data() {
        return {
            // pickerOptions,
            tableData:[],
            id:0,
            total:0,
            batchIds:[],
            searchParams:{currentPage:1,pageSize:5},
            dialogFormVisible:false,
            formData:{},
            imageUrl:'',
            type_options:[],
        };
    },
    methods:{
        async saveOrUpdate(){
            if (this.formData.id) {
                await this.updateById(this.formData)
            }else {
                await this.save(this.formData);
            }
            //  console.log("saveOrUpdate()");
            // await insert(this.formData);
            this.searchPage();
        },
        async updateById(){
            let response =await updateById(this.formData);
            if (response){
                this.$message({
                    message: '修改成功',
                    type: 'success'
                });
            }else {
                this.$message.error('修改失败');
            }
            this.searchPage();
        },
        async save(){
            console.log("aaa");
            let response =await insert(this.formData);
            if (response){
                this.$message({
                    message: '添加成功',
                    type: 'success'
                });
            }else {
                this.$message.error('添加失败');
            }
            this.searchPage();
        },
        async findById(row){
            console.log(row)
            this.formData = row;
            this.imageUrl = this.formData.brandLogo;
        },
        selectChange(brandList){
            if (brandList.length == 1){
                this.formData.id = brandList[0].id;
            }
            this.batchIds = brandList.map(item=>item.id);
        },

        showMsg(){
            showMessageBox.smb('此操作将永久删除该文件, 是否继续?', '删除成功!', '已取消删除').then(r  =>{
                r ? this.batchDeleteByIds():null
            })
        },
        selectAll(){
            findAll().then(response=>{
                this.tableData =response;
            })
        },

        //async 表示searchPage 是异步的，但是 await 表示 要等到 findPage 执行后，代码才会继续执行
        //分页查条件查询
        async searchPage(){
            let response = await findPage(this.searchParams);
                this.tableData = response.data;
                this.total=response.total;
        },
        //分页默认值
        resetForm(){
            this.searchParams = {currentPage: 1,pageSize: 5}
            this.searchPage();
        },
        currentPageChange(currentIndex){
            this.searchParams.currentPage=currentIndex;
            this.searchPage();
        },
        async deleteById(){
            let response= await deleteById(this.id);
                if (response){
                this.$message({
                    message: '删除成功',
                    type: 'success'
                    });
                 }else {
                this.$message.error('删除失败');
                 }
            await this.searchPage();
        },
        async batchDeleteByIds() {
            let response= await batchDelete(this.batchIds);
                if (response<=0){
                    this.$message({
                    message: '删除成功',
                    type: 'success'
                    });
                }else {
                    this.$message.error('删除失败');
                }

            this.searchPage();
        },
    },
    created(){
        this.searchPage()
    }
};
